/**
 * Author: wangjunfu
 * Component: Editable
 * Date: 2016-12-22
 * Description: 工单信息-关联配件
 */

(function ($) {

    'use strict';

    var EditableTable = {

        options: {
            addButton: '#addParts',
            table: '#datatable-parts'
        },

        initialize: function () {
            this
                .setVars()
                .build()
                .events();
        },

        setVars: function () {
            this.$table = $(this.options.table);
            this.$addButton = $(this.options.addButton);

            return this;
        },

        build: function () {
            this.datatable = this.$table.DataTable({
                aoColumns: [
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    {"bSortable": false}
                ]
            });

            window.dt = this.datatable;

            return this;
        },

        events: function () {
            var _self = this;

            this.$table.on('click', 'a.remove-row', function (e) {
                e.preventDefault();

                // 删除当前行
                var $row = $(this).closest('tr');
                _self.rowRemove($row);
            });

            this.$addButton.on('click', function (e) {
                e.preventDefault();

                _self.rowAdd();
            });

            return this;
        },

        // ==========================================================================================
        // ROW FUNCTIONS
        // ==========================================================================================
        rowAdd: function () {

            var _self = this,
                actions,
                data,
                $row;

            actions = [
                '<a href="javascript:void(0)" class="on-default remove-row"><i class="fa fa-trash-o"></i></a>'
            ].join(' ');

            // 新增一行
            var partId = $("input[name='tmp_partid']").val();
            var partName = '';
            var specialName = '';
            var modelName = '';
            var price = '';

            $.ajax({
                url: "/erp/purchase/getPartInfo",
                type: "POST",
                data: {partId: partId},
                dataType: 'json',
                async: false,
                error: function (request) {
                    // 提交失败
                    showInfoAlert("服务器连接失败，请检查网络环境！", null);
                },
                success: function (data) {
                    if (data.returnCode === 1) {
                        partName = data.partName;
                        specialName = data.specialName;
                        modelName = data.modalName;
                        price = data.price;
                    }

                    $("input[name='tmp_partid']").val(0);
                }
            });


            // 追加数据
            if (partName !== '') {
                data = _self.datatable.row.add([0, partId, partName, specialName, modelName, '1', price, price, price, '', actions]);
                $row = _self.datatable.row(data[0]).nodes().to$();
                $row.find('td:last').addClass('actions');

                _self.rowEdit($row);
                _self.datatable.order([0, 'asc']).draw();   // 排序
            }
        },

        rowEdit: function ($row) {
            var _self = this, data, len, ro;
            var factor_names = ["f_id2", "f_partid", "f_partname", "f_specialname", "f_modalname", "f_salecount", 'f_intoprice', 'f_saleprice', 'f_totalprice', 'f_remark2', "action"];

            data = this.datatable.row($row.get(0)).data();

            $row.children('td').each(function (i) {
                var $this = $(this);

                len = "style='width: 120px;'";

                if (i === 2) {
                    len = "style='width: 300px;'";
                }
                if (i === 3 || i === 4) {
                    len = "style='width: 150px;'";
                }
                if (i === 9) {
                    len = "style='width: 100%;'";
                }
                if (i === 10) {
                    len = "style='width: 100px;'";
                }

                if (i === 0 || i === 1 || i === 2) {
                    ro = "readonly";
                } else {
                    ro = "";
                }

                if ($this.hasClass('actions')) {
                    $this.attr("style", "text-align: center");
                } else {
                    if (i === 0 || i === 1) {
                        $this.attr("style", "display: none");
                    }
                    $this.html("<input type='text' id='" + factor_names[i] + "' name='" + factor_names[i] + "' class='form-control input-block' value='" + data[i] + "' " + ro + " " + len + " />");
                }
            });
        },

        rowRemove: function ($row) {
            this.datatable.row($row.get(0)).remove().draw();
        }
    };

    $(function () {
        EditableTable.initialize();
    });

}).apply(this, [jQuery]);